<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "users".
 *
 * @property int $id 主键
 * @property string $username 用户名
 * @property string $password 密码
 * @property string $headimgurl 头像
 * @property int $sex 性别  1 男   2 女
 * @property string $mobile 手机号
 * @property int $status 状态 1 有效   0 无效
 * @property string $ctime 创建时间
 * @property string $utime 更新时间
 * @property string $last_login 最后登录时间
 * @property string $last_ip 最后一次登录ip
 * @property string $mark 备注
 * @property string $invite_code 邀请码
 * @property string $parent_invite_code 父邀请码
 * @property string $energy_number 默认添加活力值
 */
class Users extends BaseModel implements \yii\web\IdentityInterface {
    const STATUS_VALID = 1;
    const STATUS_UNVALID = 0;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return 'users';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['sex', 'status'], 'integer'],
            [['ctime', 'utime', 'last_login'], 'required'],
            [['ctime', 'utime', 'last_login'], 'safe'],
            [['username'], 'string', 'max' => 80],
            [['password'], 'string', 'max' => 32],
            [['headimgurl'], 'string', 'max' => 200],
            [['mobile'], 'string', 'max' => 30],
            [['last_ip'], 'string', 'max' => 15],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'username' => 'Username',
            'password' => 'Password',
            'headimgurl' => 'Headimgurl',
            'sex' => 'Sex',
            'mobile' => 'Mobile',
            'status' => 'Status',
            'ctime' => 'Ctime',
            'utime' => 'Utime',
            'last_login' => 'Last Login',
            'last_ip' => 'Last Ip',
        ];
    }

    /*
     * Finds an identity by the given ID
     * **/
    public static function findIdentity($id) {
        return self::findOne(['id' => $id, 'status' => self::STATUS_VALID]);
    }

    /*
     * Returns an ID that can uniquely identify a users identity
     * **/

    public function getId() {
        return $this->getPrimaryKey();
    }

    public static function findIdentityByAccessToken($token, $type = null) {

    }

    public function getAuthKey() {
    }

    public function validateAuthKey($authKey) {
    }

    public function getUsersProduct() {
        return $this
            ->hasMany(UsersProduct::class, ['user_id' => 'id']);
//            ->where([
//                'and',
//                ['users_product.status' => 1],
//                ['>=', 'ctime', date("Y-m-d 00:00:00")],
//                ['<=', 'ctime', date("Y-m-d 23:59:59")],
//            ]);
    }

    public function getAppointProduct() {
        return $this
            ->hasMany(AppointProduct::class, ['user_id' => 'id'])
            ->where([
                'and',
                ['appoint_product.status' => 1],
                ['=', 'date', date("Y-m-d 00:00:00")],
            ]);
    }

    public function getUserTask() {
        return $this
            ->hasMany(UserTask::class, ['user_id' => 'id'])
            ->where([
                'and',
                ['user_task.status' => 1],
                ['=', 'date', date("Y-m-d")],
            ]);
    }

    public function getDealOrders() {
        return $this
            ->hasMany(Orders::class, ['buy_user_id' => 'id'])
            ->where([
                'and',
                ['orders.status' => [1, 2]],
            ])
            ->orderBy('id DESC');
    }

    public function getComplaintOrders() {
        return $this
            ->hasMany(Orders::class, ['buy_user_id' => 'id'])
            ->where([
                'and',
                ['orders.status' => [3, 4]],
            ])
            ->orderBy('id DESC');
    }

    public function getSellerOrders() {
        return $this
            ->hasMany(Orders::class, ['sellers_user_id' => 'id'])
            ->where([
                'and',
                ['orders.status' => [1, 2]],
            ])
            ->orderBy('id DESC');
    }

    public function getSellerComplaintOrders() {
        return $this
            ->hasMany(Orders::class, ['sellers_user_id' => 'id'])
            ->where([
                'and',
                ['orders.status' => 4],
            ])
            ->orderBy('utime DESC');
    }

    public function getSellerComplaintAppealOrders() {
        return $this
            ->hasMany(Orders::class, ['sellers_user_id' => 'id'])
            ->where([
                'and',
                ['orders.status' => 3],
            ])
            ->orderBy('utime DESC');
    }

    public function getCustomerToUsers() {
        return $this->hasMany(CustomerServiceMessages::class, ['to_user_id' => 'id']);
    }

    public function getCustomerFormUsers() {
        return $this->hasMany(CustomerServiceMessages::class, ['from_user_id' => 'id']);
    }

    public function getArchiveLogs() {
        return $this
            ->hasMany(ArchiveLogs::class, ['user_id' => 'id']);
    }

    public function getEnergyLogs() {
        return $this->hasMany(EnergyLogs::class, ['user_id' => 'id'])->orderBy('id DESC');
    }

    public function getPayAccount() {
        return $this->hasMany(PayAccounts::class, ['user_id' => 'id'])->where([
            'and',
            ['status' => [0, 1,-2]],
        ]);
    }

    public function getUsersPayAccount() {
        return $this->hasMany(PayAccounts::class, ['user_id' => 'id'])->where([
            'and',
            ['status' => 1],
        ]);
    }
}
